const { getTagListSv, createTagSv } = require('../service/tag.service')
const { serverError } = require('../constants/error.type')

const getTagList = async (ctx, next) => {
  try {
    const result = await getTagListSv()
    ctx.body = {
      code: 0,
      message: '获取成功',
      data: result
    }
  } catch (err) {
    console.error('[getTagList error]: ', err)
    return ctx.app.emit('error', serverError, ctx)
  }

  await next()
}

const createTag = async (ctx, next) => {
  const { name } = ctx.request.body
  try {
    await createTagSv({ name })
    ctx.body = {
      code: 0,
      message: '创建成功'
    }
  } catch (err) {
    console.error('[getTagList error]: ', err)
    return ctx.app.emit('error', serverError, ctx)
  }

  await next()
}

module.exports = {
  getTagList,
  createTag,
}
